﻿/*
Copyright (c) 2005-2008, CellAO Team

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    * Neither the name of the CellAO Team nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Reflection;
using System.Text;
using AO.Core;
using AO.Core.Config;
using Cell.Core;
using NLog;
using NLog.Config;
using NLog.Targets;
using NLog.Win32.Targets;

namespace ChatEngine
{
    /// <summary>
    /// 
    /// </summary>
    class Program
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            #region Console Texts...

            string assversion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
            Console.Title = "CellAO ChatEngine Console. Version: Shining_Coiler - " + assversion.ToString();
            Console.WriteLine("========================================================");
            Console.WriteLine("CellAO copyright 2008 by the CellAO Dev team");
            Console.WriteLine("We are using BSD Licensing for our Core files");
            Console.WriteLine("Cell Framework is copyright under GPL");
            Console.WriteLine("Anarchy Online is copyright by Funcom");
            Console.WriteLine("Thank you for choosing CellAO");
            Console.WriteLine("========================================================");
            Console.WriteLine("Loading ChatEngine...");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Using ISComm v1.0");
            Console.WriteLine("[OK]");
            Console.ResetColor();

            #endregion

            Server ChatServer = new Server();

            System.Console.WriteLine("[ISComm] Waiting for link...");
            if (!ZoneCom.Link(ConfigReadWrite.Instance.CurrentConfig.ZoneIP, 9669, ChatServer))
            {
                System.Console.WriteLine("[ISComm] Unable to link to ZoneEngine. :(");
                return;
            }
            System.Console.WriteLine("[ISComm] Linked with ZoneEngine! :D");

            ChatServer.EnableTCP = true;
            ChatServer.EnableUDP = true;

            try { ChatServer.TcpIP = IPAddress.Parse(ConfigReadWrite.Instance.CurrentConfig.ListenIP); }
            catch { Console.WriteLine("Error parsing my ip address. Check config file!"); Console.ReadKey(); return; }
            try { ChatServer.UdpIP = IPAddress.Parse(ConfigReadWrite.Instance.CurrentConfig.ListenIP); }
            catch { Console.WriteLine("Error parsing my ip address. Check config file!"); Console.ReadKey(); return; }

            ChatServer.TcpPort = Convert.ToInt32(ConfigReadWrite.Instance.CurrentConfig.ChatPort);
            ChatServer.UdpPort = Convert.ToInt32(9878); //??!!?!?!?!?!?!?

            #region NLog

            LoggingConfiguration config = new LoggingConfiguration();
            ColoredConsoleTarget consoleTarget = new ColoredConsoleTarget();
            consoleTarget.Layout = "${date:format=HH\\:MM\\:ss} ${logger} ${message}";
            FileTarget fileTarget = new FileTarget();
            config.AddTarget("file", fileTarget);
            fileTarget.FileName = "${basedir}/ChatEngineLog.txt";
            fileTarget.Layout = "${date:format=HH\\:MM\\:ss} ${logger} ${message}";
            LoggingRule rule1 = new LoggingRule("*", LogLevel.Trace, consoleTarget);
            config.LoggingRules.Add(rule1);
            LoggingRule rule2 = new LoggingRule("*", LogLevel.Trace, fileTarget);
            config.LoggingRules.Add(rule2);
            LogManager.Configuration = config;

            #endregion

            ChatServer.MaximumPendingConnections = 100;

            string consoleCommand;
            Console.WriteLine("Enter \"start\" to start server, \"stop\" to stop it and \"exit\" to exit program");
            while (true)
            {
                consoleCommand = Console.ReadLine();
                switch (consoleCommand.ToLower())
                {
                    case "start":
                        ThreadMgr.Start();
                        ChatServer.Start();
                        break;
                    case "stop":
                        ThreadMgr.Stop();
                        ChatServer.Stop();
                        break;
                    case "exit":
                        Process.GetCurrentProcess().Kill();
                        break;
                    default:
                        Console.WriteLine("Unknown command\n");
                        Console.WriteLine("Available commands:");
                        Console.WriteLine("----------------------");
                        Console.WriteLine("start - starts server.");
                        Console.WriteLine("stop  - stops server.");
                        Console.WriteLine("exit  - exits program.");
                        Console.WriteLine("----------------------\n");
                        break;
                }
            }
        }
    }
}